<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Config;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ConfigController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Config(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('type')->using([1 => '行文本', 2 => '富文本']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Config(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type');
            $show->field('value');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Config(), function (Form $form) {
            $form->text('name')->required();
            $form->radio('type')->options([1 => '行文本', 2 => '富文本'])
                ->when(1, function (Form $form) {
                    $form->text('value');
                })
                ->when(2, function (Form $form) {
                    $form->editor('value2', '值')->value($form->model()->value);
                })->required();
        })->saving(function (Form $form) {
            if ($form->input('type') == 1) {
                $form->value = $form->input('value');
                $form->deleteInput('value2');
            } elseif ($form->input('type') == 2) {
                $form->value = $form->input('value2');
                $form->deleteInput(['value2']);
            }
        });
    }
}
